﻿package com.oyjd.beauty.business.recommand.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.oyjd.beauty.bean.vo.UserVO;
import com.oyjd.beauty.business.goods.vo.GoodsVOEx;
import com.oyjd.beauty.business.recommand.service.recommandService;
import com.oyjd.beauty.fw.action.BaseAction;
import com.oyjd.beauty.fw.action.Page;
import com.oyjd.beauty.fw.util.CodeUtil;
import com.oyjd.beauty.fw.util.OutUtil;
import com.oyjd.beauty.pub.Const;

/**
 * 
 * 店长推荐Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/recommand")
@Results({ @Result(name = "viewList", location = Const.JSP_recommand_order), @Result(name = "viewLeftList", location = Const.JSP_recommand_orderDx) })
public class recommandAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 45354347027015039L;

	private static final Log log = LogFactory.getLog(recommandAction.class);
	private recommandService service;

	@Autowired
	public void setService(recommandService service) {
		this.service = service;
	}

	/**
	 * 进入排序信息列表页面
	 * 
	 * @return
	 */
	public String viewList() {
		return "viewList";
	}

	/**
	 * 进入排序差异信息列表页面
	 * 
	 * @return
	 */
	public String viewLeftList() {
		return "viewLeftList";
	}

	/**
	 * 交换资料的排序位置
	 * 
	 * @return
	 */
	public String exchangePos() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String id1 = ServletRequestUtils.getStringParameter(req, "id1");
			String id2 = ServletRequestUtils.getStringParameter(req, "id2");
			service.exchangePos(id1, id2);
			//
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 将资料的排至首位
	 * 
	 * @return
	 */
	public String moveFirst() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String id = ServletRequestUtils.getStringParameter(req, "id");
			service.moveFirst(id);
			//
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 新增类型排序元素
	 * 
	 * @return
	 */
	public String addOrder() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String[] cbs = ServletRequestUtils.getStringParameters(req, "cb");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.addOrder(cbs, userVO.getAccount());
			//
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 删除类型排序元素
	 * 
	 * @return
	 */
	public String removeOrder() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();

			String[] cbs = ServletRequestUtils.getStringParameters(req, "cb");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.removeOrder(cbs, userVO.getAccount());
			//
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 查询列表
	 * 
	 * @return
	 */
	public String getList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<GoodsVOEx> page = service.getList(new Page<GoodsVOEx>(req));
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 获取差异的列表
	 * 
	 * @return
	 */
	public String getLeftList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<GoodsVOEx> page = service.getLeftList(new Page<GoodsVOEx>(req));
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}
}
